
// button object
local PANEL = {};


/*------------------------------------
	initialize compact mode
------------------------------------*/
function PANEL:Init( )

	// set the size
	self:SetSize( 270, 90 );
	
	// create media info area
	self.MediaInfo = vgui.Create( "MXMediaInfo" , self );

	// set tooltip
	self.btnClose:SetTooltip( "#musex_exit" );
	
	// create minimize button
	self.Minimize = vgui.Create( "DSysButton", self );
	// set the icon
	self.Minimize:SetType( "down" );
	// dont draw default shit
	self.Minimize:SetDrawBorder( false );
	self.Minimize:SetDrawBackground( false );
	// set the tooltip
	self.Minimize:SetTooltip( "#musex_hide" );
	// setup the minimize command
	self.Minimize.DoClick = function ( button ) self:Hide(); end
	
	// create viewmode button
	self.ViewMode = vgui.Create( "DSysButton", self );
	// change the icon
	self.ViewMode:SetType( "grip" );
	// disable default border and background
	self.ViewMode:SetDrawBorder( false );
	self.ViewMode:SetDrawBackground( false );
	// set the tooltip
	self.ViewMode:SetTooltip( "#musex_switch_normal" );
	// setup the viewmode command
	self.ViewMode.DoClick = function ( button )
	
		// set to normal mode
		MuseX.SetSetting( "viewmode", 1 );
		
		// restart MuseX
		MuseX.Restart( true );
		
	end

	// build the window title
	self:SetTitle( ("MuseX version %s"):format( MuseX.GetVersion() ) );
	
	// setup the window
	self:SetDraggable( true );
	self:SetDeleteOnClose( false );
	
	
	// create the control object
	self.Controls = vgui.Create( "MXControls", self );
	self.Controls.MediaInfo:Remove();
	self.Controls.Shuffle:Remove();
	self.Controls.Repeat:Remove();
	self.Controls.PlaylistInfo:Remove();
	
	// create the options button
	self.Options = vgui.Create( "MXButton", self );
	// tooltip
	self.Options:SetText( "..." );
	// setup the options command
	self.Options.DoClick = function( self ) MuseX.SettingsMenu( true ); end;
	
	// create the playlist
	self.Playlist = vgui.Create( "MXPlaylist" );
	self.Playlist:SetVisible( false );
	
	// custom derma hooks
	Derma_Hook( self, "Paint", "Paint", "MXCompactFrame" );
	Derma_Hook( self, "ApplySchemeSettings", "Scheme", "MXCompactFrame" );
	self.AnimIn = Derma_Anim( "OpenAnim", self, self.OpenAnim );
	self.AnimOut = Derma_Anim( "CloseAnim", self, self.CloseAnim );

end


/*------------------------------------
	open the main window
------------------------------------*/
function PANEL:Open( )

	// ignore if we're already visible
	if ( self:IsVisible() ) then
	
		// poof
		return;
		
	end
	
	// close menus
	CloseDermaMenus();
	
	// clear flag
	self.IsHiding = false;
	
	// take focus and be visible
	self:MakePopup();
	self:SetVisible( true );
	
	// disable keyboard input
	self:SetKeyboardInputEnabled( false );
	
	// enable mouse input
	self:SetMouseInputEnabled( true );
	
	// halt the close animation
	self.AnimOut:Stop();
	
	// start the open animation
	self.AnimIn:Start( 0.1 );

end


/*------------------------------------
	handle the opening animation
------------------------------------*/
function PANEL:OpenAnim( anim, delta, data )

	// check if it has been started
	if ( anim.Started ) then
	
		// reset alpha
		self:SetAlpha( 0 );
		
	end
	
	// check if it has been finished
	if ( anim.Finished ) then
	
		// fully opaque
		self:SetAlpha( 255 );
		
		// all done
		return;
		
	end
	
	// animate the alpha
	self:SetAlpha( 255 * delta );

end


/*------------------------------------
	close the main window
------------------------------------*/
function PANEL:Close( skip, ishiding )
	
	// close menus
	CloseDermaMenus();
	
	// save flag
	self.IsHiding = ishiding;
	
	// halt the opening animation
	self.AnimIn:Stop();
	
	// check if we're skipping the animation
	if ( skip ) then
	
		// restore alpha
		self:SetAlpha( 255 );
		
		// not visible
		self:SetVisible( false );
	
	else
	
		// start the close animation
		self.AnimOut:Start( 0.1 );
		
	end

end


/*------------------------------------
	hide the main window
------------------------------------*/
function PANEL:Hide( )
	
	// pass to close function
	self:Close( true, true );

end


/*------------------------------------
	handle the closing animation
------------------------------------*/
function PANEL:CloseAnim( anim, delta, data )

	// check if the animation has started
	if ( anim.Started ) then
	
		// restore the alpha
		self:SetAlpha( 255 );
		
	end
	
	// check if the animation has finished
	if ( anim.Finished ) then
	
		// store alpha
		self:SetAlpha( 255 );
		
		// set as not visible
		self:SetVisible( false );
		
		// check if we're exiting
		if ( !self.IsHiding ) then
		
			// close MuseX
			MuseX.Close();
		
		end
		
		// 2girls1cup.com
		return;
		
	end
	
	// animate the alpha
	self:SetAlpha( 255 - 255 * delta );

end


/*------------------------------------
	layout the main window
------------------------------------*/
function PANEL:PerformLayout( )

	// let the BaseClass do its stuff
	self.BaseClass.PerformLayout( self );
	
	// size & position minimize button
	self.Minimize:SetPos( self:GetWide() - 40, 4 )
	self.Minimize:SetSize( 18, 18 );
	
	// size & position viewmode button
	self.ViewMode:SetPos( self:GetWide() - 58, 4 )
	self.ViewMode:SetSize( 18, 18 );
	
	// size & position media info
	self.MediaInfo:SetSize( self:GetWide() - 10, 20 );
	self.MediaInfo:SetPos( 5, 30 );
	
	// size & position the controls
	self.Controls:SetSize( self:GetWide(), 50 );
	self.Controls:SetPos( 0, self:GetTall() - self.Controls:GetTall() );
	
	// size & position options button
	self.Options:SetSize( 25, 16 );
	self.Options:SetPos( 5, self:GetTall() - 24 );
	
end


/*------------------------------------
	main window think
------------------------------------*/
function PANEL:Think( )

	// call BaseClass
	self.BaseClass.Think( self );
	
	// check if they finishing moving the window
	if ( !self.Dragging && self.WasDragging ) then
	
		// update position in settings
		MuseX.SetSetting( "compactwindowx", self.X );
		MuseX.SetSetting( "compactwindowy", self.Y );
		
	end
	
	// update previous dragging state
	self.WasDragging = self.Dragging;
	
	// run the animation functions
	self.AnimIn:Run();
	self.AnimOut:Run();

end








/*------------------------------------
	add a list of songs to playlist
------------------------------------*/
function PANEL:AddToPlaylist( songs )

	// loop through list of songs
	for _, song in pairs( songs ) do
	
		// add song
		self.Playlist:AddSong( song );
	
	end
	
end


/*------------------------------------
	remove a list of songs from
	the playlist
------------------------------------*/
function PANEL:RemoveFromPlaylist( songs )

	// loop through list of songs
	for _, song in pairs( songs ) do
	
		// remove song
		self.Playlist:RemoveSong( song );
	
	end
	
end


/*------------------------------------
	clear all songs from playlist
------------------------------------*/
function PANEL:ClearPlaylist( )

	// pass to playlist object
	self.Playlist:ClearPlaylist( );

end


/*------------------------------------
	get all songs on playlist
------------------------------------*/
function PANEL:GetPlaylist( )

	// pass to playlist
	return self.Playlist:GetPlaylist();

end


/*------------------------------------
	get the total count of songs
------------------------------------*/
function PANEL:GetPlaylistCount( )

	// pass to playlist
	return self.Playlist:GetPlaylistCount();

end


/*------------------------------------
	get the song a the specified
	playlist position
------------------------------------*/
function PANEL:GetPlaylistSong( num )

	// pass to playlist
	return self.Playlist:GetPlaylistSong( num );

end


/*------------------------------------
	get the currently highlighted
	playlist song
------------------------------------*/
function PANEL:GetSelectedPlaylistSong( )

	// pass to playlist
	return self.Playlist:GetSelectedPlaylistSong();
	
end


/*------------------------------------
	gets a songs position in list
------------------------------------*/
function PANEL:GetPlaylistPosition( filename )

	// pass to playlist
	return self.Playlist:GetPlaylistPosition( filename );
	
end


/*------------------------------------
	set the media info table
------------------------------------*/
function PANEL:SetMediaInfo( tbl )

	// pass to media info
	self.MediaInfo:SetMediaInfo( tbl );

end


/*------------------------------------
	show playlist info
------------------------------------*/

function PANEL:ShowPlaylistInfo( text )

	// pass to controls
//	self.Controls:ShowPlaylistInfo( text );

end


/*------------------------------------
	refresh the volume bar position
------------------------------------*/
function PANEL:RefreshVolumePosition( )

	// pass to controls
	self.Controls:RefreshVolumePosition();

end


/*------------------------------------
	open visualization hotkey editor
------------------------------------*/
function PANEL:OpenHotKeyEditor( )

	// check if the hotkey editor has been opened
	if ( !self.HotKeyEditor || !self.HotKeyEditor:IsValid() ) then
	
		// if not, create it
		self.HotKeyEditor = vgui.Create( "MXHotKeyEditor" );
		
		// not visible
		self.HotKeyEditor:SetVisible( false );
		
		// set position
		self.HotKeyEditor:Center();
		
	end
	
	// open
	self.HotKeyEditor:Open();
	
end


/*------------------------------------
	set the position of the seek bar
------------------------------------*/
function PANEL:SetSeekPosition( frac )

	// pass to controls panel
	self.Controls:SetSeekPosition( frac );

end


// define
derma.DefineControl( "MXCompact", "", PANEL, "DFrame" );
